/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.glc.hms.core.common;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.glc.hms.common.tree.MenuTreeInfo;
import com.glc.hms.holder.SpringContextHolder;
import com.glc.hms.system.dao.SysDeptMapper;
import com.glc.hms.system.dao.SysUserRoleMapper;
import com.glc.hms.system.entity.SysDept;
import com.glc.hms.system.entity.SysMenuaction;
import com.glc.hms.system.entity.SysRole;
import com.glc.hms.system.service.ISysMenuService;
import com.glc.hms.system.service.ISysRoleMenuService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 常量的生产工厂
 * @author Santa
 * @since 2019/11/14
 */
@Slf4j
@Component
@DependsOn("springContextHolder")
public class ConstantFactory {


    public static ConstantFactory me() {
        return SpringContextHolder.getBean("constantFactory");
    }


    private SysUserRoleMapper userRoleMapper = SpringContextHolder.getBean(SysUserRoleMapper.class);
    private SysDeptMapper deptMapper = SpringContextHolder.getBean(SysDeptMapper.class);
    private ISysMenuService menuService = SpringContextHolder.getBean(ISysMenuService.class);
    private ISysRoleMenuService roleMenuService = SpringContextHolder.getBean(ISysRoleMenuService.class);


    /**
     * 根据用户id查询角色信息
     * @param userId
     * @return
     */
    public SysRole getRoleByUserId(Long userId){
        SysRole sysRole = userRoleMapper.queryRoleByUserId(userId);
        return sysRole;
    }


    /**
     * 根据部门id获取部门名称
     * @param deptId
     * @return
     */
    public String getDeptNameById(Long deptId){
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id",deptId);
        SysDept sysDept = deptMapper.selectOne(queryWrapper);
        if(sysDept!=null){
            return sysDept.getName();
        }
        return null;
    }


    /**
     * 获取部门下的子部门(包括寄己)
     * @param deptId
     * @return
     */
    public List<Long> queryChileDeptId(Long deptId){
        List<Long> list = Lists.newArrayList();
        list.add(deptId);
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", deptId);
        List<SysDept> sysDepts = deptMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(sysDepts)){
            for (SysDept sysDept : sysDepts) {
                List<Long> longs = this.queryChileDeptId(sysDept.getDeptId());
                list.addAll(longs);
            }
        }
        return list;
    }

    /**
     * 根据角色id获取对应的菜单
     * @param roleId
     * @return
     */
    public List<MenuTreeInfo> getMenuListByRoleId(Long roleId){
        List<MenuTreeInfo> menuTreeInfos = menuService.queryMenuList(roleId);
        //将二级菜单的操作权限添加进去
        if(!CollectionUtils.isEmpty(menuTreeInfos)){
            for (MenuTreeInfo menuTreeInfo : menuTreeInfos) {
                if (menuTreeInfo.getChildren()!=null) {
                    List<MenuTreeInfo> children = menuTreeInfo.getChildren();
                    for (MenuTreeInfo child : children) {
                        List<SysMenuaction> sysMenuactions = roleMenuService.queryActionByRoleIdAndMenuId(roleId, child.getId());
                        List<MenuTreeInfo> list = Lists.newArrayList();
                        if(!CollectionUtils.isEmpty(sysMenuactions)){
                            for (SysMenuaction sysMenuaction : sysMenuactions) {
                                MenuTreeInfo menuTreeInfo1 = new MenuTreeInfo();
                                //为了避免前端问题 此处将menuId和ActionId拼接
                                Long id = Long.valueOf(child.getId().toString() + sysMenuaction.getActionId());
                                menuTreeInfo1.setId(id);
                                menuTreeInfo1.setMenuTitle(sysMenuaction.getActionName());
                                list.add(menuTreeInfo1);
                            }
                        }
                        child.setChildren(list);
                    }
                }
            }
        }
        return menuTreeInfos;
    }

}